package com.controller;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.hibernate.SessionFactory;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dao.UsersDaoImpl;
import com.entity.User;
import com.service.UserServiceImpl;

@Controller
public class UserController {
	@Resource
	private UserServiceImpl userServiceImpl;
	
	/*
	 * 注册页面跳转，先进行验证
	 */
	@RequestMapping("register")
	public String checkUserRegister(HttpServletResponse response,HttpServletRequest request, User user) throws IOException,ServletException{
		//获取register.jsp文件中name的属性
		System.out.println(user);
		String userRegister = userServiceImpl.userRegister(user);
		System.out.println(userRegister);
		if(userRegister.equals("0")){//注册成功，待激活
			//(没写激活)
			request.setAttribute("msg", "注册成功");
			return "login.jsp";
		}
		return "/login.jsp";
		
	}
	
	/*
	 *登录 
	 */
	@RequestMapping("/user_login")
	public String userlogin(HttpServletRequest request,HttpServletResponse response) throws IOException{
		response.setContentType("text/html;charset=utf-8");
		HttpSession session = request.getSession();
		String phone = request.getParameter("phone");
		String password = request.getParameter("password");
		String remember = request.getParameter("remember");
		boolean a = this.userServiceImpl.getByPhone(phone);
		boolean b = this.userServiceImpl.Login(phone, password);
		if(a == false){
			request.setAttribute("message1", "请输入正确用户名");
			return "/login.jsp";
		}else if(b == false){
			request.setAttribute("message2", "请输入正确密码");
			return "/login.jsp";
		}else{
			if(remember != null){
				User u = this.userServiceImpl.findByPhone(phone);
				session=request.getSession();
				session.setMaxInactiveInterval(60*60*24);
				session.setAttribute("u", u);
				System.out.println("记住的用户"+session.getAttribute("u"));
				return "shop.jsp";
			}
			System.out.println("测试"+remember);
			
			return "shop.jsp";
		}
		
	}
	
	/*
	 *退出登录
	 */	
	
	@RequestMapping("user_loginout")
	public String loginOut(HttpServletRequest request,HttpServletResponse response,String phone,User user) throws ServletException,IOException{
		System.out.println("退出登录");
		
		HttpSession session = request.getSession();
		session.getAttribute("u");
		
		if(session.getAttribute("u")!=null){
			request.getAttribute("u");
			session.getAttribute("u");
			System.out.println("退出，已经记住密码");
			return "login.jsp";
		}else{
			session.removeAttribute("u");
//			request.setAttribute("msg", "退出成功");
			System.out.println("退出，没有记住密码");
			return "login.jsp";
		}
		
		
	}
	
	/*
	 * 记住密码
	 */
//	@RequestMapping("remember")
//	public String RemPwd(HttpServletRequest request) throws ServletException{
//		
//		return null;
//	}
	
	
	/*
	 * 更新信息
	 */
	@RequestMapping("user_update")
	//修改先查询到相应的用户信息,修改后返回来再存进数据库
	public String userUpdate(String phone,HttpServletRequest request,HttpServletResponse response)throws IOException, ServletException{
		User user = userServiceImpl.findByPhone(phone);
		System.out.println(user);
		//获取修改后的信息，再返回前端
		HttpSession session = request.getSession();
		session.setAttribute("u", user);
		return "userinfo.jsp";	
	}
	
	@RequestMapping("update")
	public String Update(User user,HttpServletRequest request){
		HttpSession session = request.getSession();
		String userUpdate = userServiceImpl.userUpdate(user);
		System.out.println(userUpdate);
		user = this.userServiceImpl.getUserByPhone(user.getPhone());
		session.setAttribute("u", user);
		return "userinfo.jsp";
		
	}
	
	/*
	 * 注销账号
	 */
	@RequestMapping("logout")
	public String logOut(HttpServletRequest request,HttpServletResponse response,User user) throws ServletException{
		
		HttpSession session = request.getSession();
		session.getAttribute("u");
		
		user = this.userServiceImpl.getUserByPhone(user.getPhone());
		System.out.println("获取到的用户"+user);
		boolean a = userServiceImpl.userDelete(user);
		if(a !=false ){
			request.setAttribute("msg", "注销失败");
			return "userinfo.jsp";
		}else{
			session.removeAttribute("u");
			return "login.jsp";
		}
	}
}
